package com.order.itemcategoryproject.controller;

import com.order.itemcategoryproject.pojo.DBlOrderHdOnline;
import com.order.itemcategoryproject.pojo.VO.dArcItemVo;
import com.order.itemcategoryproject.pojo.dArcItem;
import com.order.itemcategoryproject.pojo.dArcStaff;
import com.order.itemcategoryproject.pojo.dBlOrderDtOnline;
import com.order.itemcategoryproject.service.DBlOrderDtOnlineService;
import com.order.itemcategoryproject.service.DBlOrderHdOnlineService;
import com.order.itemcategoryproject.service.dArcItemService;
import com.order.itemcategoryproject.service.dArcStaffService;
import com.order.itemcategoryproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.order.itemcategoryproject.utils.ParseUrl.getUrl;
import static com.order.itemcategoryproject.utils.getErrorInfoByLan.getErrorInfoByLanguage;

@RestController
@RequestMapping("/item")
public class dArcItemController {


    @Autowired
    private dArcItemService dArcItemService;

    @Autowired
    private DBlOrderHdOnlineService DBlOrderHdOnlineService;
    @Autowired
    private DBlOrderDtOnlineService DBlOrderDtOnlineService;
    @Autowired
    private dArcStaffService dArcStaffService;


    /**
     * 根据小类编号获取该小类下所有商品
     * 通过用户下的订单 获取到该用户下的所有订单
     * 根据staffId获取
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/selectAllItemByCid")
    public AjaxResult selectAllItemByCid(@RequestParam("searchValue") String searchValue, @RequestParam("categoryId") String categoryId,
                                         @RequestParam("futureDates") String futureDates,
                                         @RequestParam("orderId") String orderId,
                                         @RequestParam("currentLanguage") String currentLanguage,
                                         HttpServletRequest request) throws ParseException {

        // 检查 orderId 是否为空字符串，并将其转换为 null
        if (orderId != null && orderId.isEmpty()) {
            orderId = null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date sqlDate = null;
        Date futureTime = dateFormat.parse(futureDates);
        sqlDate = new java.sql.Date(futureTime.getTime());
        // 根据制单人名称获取该客户下所有的订单详情信息
        String queryString = request.getQueryString();
        String createStaffId = getUrl(queryString, "StaffID");
        // 根据制单人编号获取客户名称
        dArcStaff staffInfo = null;
        if (StringUtils.isNotBlank(createStaffId)) {
            staffInfo = dArcStaffService.getStaffInfoByStaId(createStaffId);
        }
        // 根据制单人编号获取该制单下所有下的订单明细信息
        List<dBlOrderDtOnline> byCreateStaff = null;
        if (StringUtils.isNotBlank(orderId)) {
//            byCreateStaff = DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaff(staffInfo.getStaffName());
            byCreateStaff = DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaffAndOrderId(staffInfo.getStaffName(), orderId);
        }
        List<dArcItem> dArcItems = null;

        // 判断当前客户分类是否为空
        if (StringUtils.isBlank(staffInfo.getCus_segmentation())) {
            return AjaxResult.error("当前客户不属于国际客户，还未对国内客户完成建档分类操作!!!!!");
        }
        if (StringUtils.isNotBlank(searchValue)) {
            dArcItems = dArcItemService.selectAllItemBySmCategory(searchValue.trim(),
                    categoryId, createStaffId, sqlDate, currentLanguage, staffInfo.getCus_segmentation());
        } else {
            dArcItems = dArcItemService.selectAllItemBySmCategory(null,
                    categoryId, createStaffId, sqlDate, currentLanguage,
                    staffInfo.getCus_segmentation());
        }
        // 获取到所有的订单明细信息 遍历当前已将添加数量的商品
        if (ObjectUtils.isNotEmpty(byCreateStaff) && ObjectUtils.isNotEmpty(dArcItems)) {
            for (int i = 0; i < dArcItems.size(); i++) {
                for (int j = 0; j < byCreateStaff.size(); j++) {
                    if (!byCreateStaff.get(j).getItemId().equals(dArcItems.get(i).getItemId())) {
                        continue;
                    }
                    dArcItems.get(i).setOrderQuantity(byCreateStaff.get(j).getItemOrderNum());
                }
            }
        }
        if (ObjectUtils.isEmpty(dArcItems)) {
            return AjaxResult.error(" 当前小类下无库存商品可下单!!!!!!");
        }
        return AjaxResult.success(dArcItems);
    }

    /**
     * 通过商品编号获取商品信息
     *
     * @param itemId
     * @return
     */
    @GetMapping("/selectSingleItmeById/{itemId}")
    public AjaxResult selectItemById(@PathVariable String itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return AjaxResult.error("当前操作有误，请联系信息部门!!!!!");
        }
        String lang = "en";
        dArcItem dArcItem = dArcItemService.selectOneItemById(itemId, null, lang, null);
        if (ObjectUtils.isEmpty(dArcItem)) {
            return AjaxResult.error("未查询到当前商品!!!!");
        }
        return AjaxResult.success(dArcItem);
    }


    /**
     * 获取热卖商品数据
     *
     * @return
     */
    @GetMapping("/getAllHotItemInfo/{futureDates}/{orderId}/{itemGrade}/{currentLanguage}")
    public AjaxResult getAllHotItemInfo(@PathVariable String futureDates, @PathVariable String orderId,
                                        @PathVariable String itemGrade,
                                        @PathVariable String currentLanguage,
                                        HttpServletRequest request) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date sqlDate = null;
        Date futureTime = dateFormat.parse(futureDates);
        sqlDate = new java.sql.Date(futureTime.getTime());

        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");
        // 根据制单人编号获取该制单下所有下的订单明细信息
        dArcStaff staffInfo = dArcStaffService.getStaffInfoByStaId(staffID);
        List<dBlOrderDtOnline> byCreateStaff = null;
        // DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaff(staffInfo.getStaffName()); // 只根据制单人名称的
        if (StringUtils.isNotBlank(orderId)) {
            byCreateStaff = DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaffAndOrderId(staffInfo.getStaffName(), orderId);
        }
        List<dArcItem> dArcItems = null;
        if (ObjectUtils.isNotEmpty(staffInfo.getCus_segmentation())) {
            dArcItems = dArcItemService.selectAllHotItemInfoByItemGrade(staffID, sqlDate, itemGrade,
                    currentLanguage, staffInfo.getCus_segmentation());
        } else {
            return AjaxResult.error("当前客户不属于国际客户，还未对国内客户完成建档dArcItems = {ArrayList@9896}  size = 42分类操作!!!!!");
        }
        if (ObjectUtils.isEmpty(dArcItems) || dArcItems.size() == 0) {
            return AjaxResult.error("根据你当前的预计付款时间未找到与之匹配库存的热dArcItems = {ArrayList@9896}  size = 42卖商品!!!!!!");
        }
        // 获取到所有的订单明细信息 遍历当前已将添加数量的商品
        if (ObjectUtils.isNotEmpty(byCreateStaff) && ObjectUtils.isNotEmpty(dArcItems)) {
            for (int i = 0; i < dArcItems.size(); i++) {
                for (int j = 0; j < byCreateStaff.size(); j++) {
                    if (!byCreateStaff.get(j).getItemId().equals(dArcItems.get(i).getItemId())) {
                        continue;
                    }
                    dArcItems.get(i).setOrderQuantity(byCreateStaff.get(j).getItemOrderNum());
                }
            }
        }
        return AjaxResult.success(dArcItems);
    }


    /**
     * 获取热卖商品数据
     *
     * @return
     */
    @GetMapping("/getAllHotItemInfoByDate/{itemGrade}/{currentLanguage}")
    public AjaxResult getAllHotItemInfoByDate(@PathVariable("itemGrade") String itemGrade,
                                              @PathVariable("currentLanguage") String currentLanguage,
                                              HttpServletRequest request) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        java.sql.Date sqlDate = null;
//        Date futureTime = dateFormat.parse(futureDates);
//        sqlDate = new java.sql.Date(futureTime.getTime());

        if (StringUtils.isBlank(itemGrade)) {
            return AjaxResult.error("当前参数未被定义!!!!!");
        }

        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");
        // 根据制单人编号获取该制单下所有下的订单明细信息
        dArcStaff staffInfo = dArcStaffService.getStaffInfoByStaId(staffID);

        java.sql.Date futureDate = null;
        DBlOrderHdOnline dBlOrderHdOnline = DBlOrderHdOnlineService.selectNewOrderInfoDesc(staffInfo.getStaffName());
        if (ObjectUtils.isNotEmpty(dBlOrderHdOnline)) {
            futureDate = dBlOrderHdOnline.getEsPaymentDate();
        }
        List<dBlOrderDtOnline> byCreateStaff = DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaffAndOrderId(staffInfo.getStaffName(), dBlOrderHdOnline.getOrderId());
        List<dArcItem> dArcItems = null;
        if (ObjectUtils.isNotEmpty(staffInfo) && StringUtils.isNotBlank(staffInfo.getCus_segmentation())) {
            dArcItems = dArcItemService.selectAllHotItemInfoByItemGrade(staffID, futureDate, itemGrade,
                    currentLanguage, staffInfo.getCus_segmentation());
        }

        if (ObjectUtils.isEmpty(dArcItems) || dArcItems.size() == 0) {
            return AjaxResult.error("根据你当前的预计付款时间未找到与之匹配库存的热卖商品!!!!!!");
        }
        // 获取到所有的订单明细信息 遍历当前已将添加数量的商品
        if (ObjectUtils.isNotEmpty(byCreateStaff) && ObjectUtils.isNotEmpty(dArcItems)) {
            for (int i = 0; i < dArcItems.size(); i++) {
                for (int j = 0; j < byCreateStaff.size(); j++) {
                    if (!byCreateStaff.get(j).getItemId().equals(dArcItems.get(i).getItemId())) {
                        continue;
                    }
                    dArcItems.get(i).setOrderQuantity(byCreateStaff.get(j).getItemOrderNum());
                }
            }
        }
        return AjaxResult.success(dArcItems);
    }

    /**
     * 根据客户编号获取当前客户下所有系列
     *
     * @param request
     * @return
     */
    @RequestMapping("/getAllItemSeriesByStorageId")
    public AjaxResult getAllItemsSeriesByStorageId(HttpServletRequest request) {
        // 首先获取到客户编号
        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");
        if (StringUtils.isBlank(staffID)) {
            return AjaxResult.error("参数未被定义!!!!!!");
        }

        // 根据客户编码获取客户信息
        dArcStaff staffInfo = dArcStaffService.getStaffInfoByStaId(staffID);

        // 如果当前参数已经获取到 那么这个时候就可以查询数据库中所有的系列名称
        List<dArcItemVo> dArcItemVos = null;
        if (ObjectUtils.isNotEmpty(staffInfo) && StringUtils.isNotBlank(staffInfo.getCus_segmentation())) {
            dArcItemVos = dArcItemService.selectAllSeriesByItemSeries(staffID, staffInfo.getCus_segmentation());
        }
        if (ObjectUtils.isEmpty(dArcItemVos)) {
            return AjaxResult.error("当前客户下并没有查询到有库存的系列商品，终止此操作!!!!!!");
        }
        // 如果不为空，那么这个时候就可以返回到前端啦
        return AjaxResult.success(dArcItemVos);
    }

    /**
     * 根据商品系列获取当前客户所有可下单商品
     *
     * @param itemSeries
     * @return
     */
    @GetMapping("/selectAllItemByItemSeries/{futureDates}/{itemSeries}/{orderId}/{currentLanguage}")
    public AjaxResult selectAllItemByItemsSeries(@PathVariable String futureDates,
                                                 @PathVariable String itemSeries,
                                                 @PathVariable String orderId,
                                                 @PathVariable String currentLanguage,
                                                 HttpServletRequest request) throws ParseException {
        if (StringUtils.isBlank(itemSeries) || StringUtils.isBlank(futureDates) || StringUtils.isBlank(orderId)) {
            return AjaxResult.error("当前参数未被定义!!!!!");
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date sqlDate = null;
        Date futureTime = dateFormat.parse(futureDates);
        sqlDate = new java.sql.Date(futureTime.getTime());

        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");
        // 根据制单人编号获取该制单下所有下的订单明细信息
        dArcStaff staffInfo = dArcStaffService.getStaffInfoByStaId(staffID);
        if (ObjectUtils.isNotEmpty(staffInfo) && StringUtils.isNotBlank(staffInfo.getCus_segmentation())) {

        }
        List<dBlOrderDtOnline> byCreateStaff = DBlOrderDtOnlineService.getAllOrderDetailInfoByCreateStaffAndOrderId(staffInfo.getStaffName(),
                orderId);

        List<dArcItem> dArcItems = null;
        if (ObjectUtils.isNotEmpty(staffInfo) && StringUtils.isNotBlank(staffInfo.getCus_segmentation())) {
            dArcItems = dArcItemService.getAllItemSeriesBySeriesName(staffID, sqlDate, itemSeries,
                    currentLanguage, staffInfo.getCus_segmentation());
        } else {
            return AjaxResult.error("当前客户不属于国际客户，还未对国内客户完成建档分类操作!!!!!");
        }


        if (ObjectUtils.isEmpty(dArcItems) || dArcItems.size() == 0) {
            String errorInfoByLanguage = getErrorInfoByLanguage(currentLanguage);
            return AjaxResult.error(errorInfoByLanguage);
        }
        // 获取到所有的订单明细信息 遍历当前已将添加数量的商品
        if (ObjectUtils.isNotEmpty(byCreateStaff) && ObjectUtils.isNotEmpty(dArcItems)) {
            for (int i = 0; i < dArcItems.size(); i++) {
                for (int j = 0; j < byCreateStaff.size(); j++) {
                    if (!byCreateStaff.get(j).getItemId().equals(dArcItems.get(i).getItemId())) {
                        continue;
                    }
                    dArcItems.get(i).setOrderQuantity(byCreateStaff.get(j).getItemOrderNum());
                }
            }
        }
        return AjaxResult.success(dArcItems);
    }

}
